package com.learn.project2.service.admin.topic;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketGoods;
import com.learn.project2.bean.MarketTopic;
import com.learn.project2.bean.MarketTopicExample;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.mapper.MarketTopicMapper;
import com.learn.project2.model.bo.admin.topic.TopicCreateBO;
import com.learn.project2.model.bo.admin.topic.TopicUpdateBVO;
import com.learn.project2.model.vo.admin.topic.*;
import com.learn.project2.utils.BaseParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @description:
 * @author zhizhuke
 * @date 2022-01-08 08-46
 * @version 1.0
 */
@Service
@CacheConfig(cacheNames = {"marketTopic"})
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    /**
     * @description:
     * @author zhizhuke
     * @date 2022-01-08 08-47
     * @version 1.0
     */
   // @Cacheable
    @Override
    public TopicListVO list(BaseParam param, String title, String subtitle) {

        //分页查询
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        PageHelper.startPage(page, limit);

        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(param.getSort()+" "+param.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (title!=null){
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle!=null){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketTopic> list = topicMapper.selectByExample(example);

        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();


        List<TopicListInnerVO> innerVOList = new ArrayList<>();

        for (MarketTopic topic : list) {
            TopicListInnerVO topicListInnerVO = new TopicListInnerVO(topic.getId(),topic.getTitle(),topic.getSubtitle(),topic.getPrice(),
                    topic.getReadCount(),topic.getPicUrl(),topic.getSortOrder(),topic.getGoods(),topic.getAddTime(),topic.getUpdateTime(),
                    topic.getDeleted() ,topic.getContent());
            innerVOList.add(topicListInnerVO);
        }

        //封装TopicListVO
        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setTotal(((int) total));
        topicListVO.setPages(pages);
        topicListVO.setLimit(limit);
        topicListVO.setPage(page);
        topicListVO.setList(innerVOList);
        return topicListVO;
    }

    /**
     * @description:
     * @author zhizhuke
     * @date 2022-01-08 08-47
     * @version 1.0
     */
    @Override
    public TopicCreateVO create(TopicCreateBO tcb) {
        MarketTopic topic = new MarketTopic(null, tcb.getTitle(), tcb.getSubtitle(), new BigDecimal(tcb.getPrice()), tcb.getReadCount(), tcb.getPicUrl(),
                null, tcb.getGoods(), new Date(), new Date(), false, tcb.getContent());
        topicMapper.insertSelective(topic);
        return new TopicCreateVO(topic.getId(),topic.getTitle(),topic.getSubtitle(), Integer.parseInt(topic.getPrice().toString()),topic.getReadCount()
        ,topic.getPicUrl(),topic.getGoods(),topic.getAddTime(),topic.getUpdateTime(),topic.getContent());
    }

    /**
     * @description:
     * @author zhizhuke
     * @date 2022-01-08 08-47
     * @version 1.0
     */
    @Transactional
 //   @Cacheable
    @Override
    public TopicReadVO read(Integer id) {
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id).andDeletedEqualTo(false);


        List<MarketTopic> topics = topicMapper.selectByExample(example);
        MarketTopic topic = topics.get(0);
        TopicListInnerVO topicListInnerVO = new TopicListInnerVO(topic.getId(),topic.getTitle(),topic.getSubtitle(),topic.getPrice(),
                topic.getReadCount(),topic.getPicUrl(),topic.getSortOrder(),topic.getGoods(),topic.getAddTime(),topic.getUpdateTime(),
                topic.getDeleted() ,topic.getContent());


        List<TopicReadGListVO> goodslist = new ArrayList<>();
        Integer[] gIds = topic.getGoods();
        if (gIds!=null){
            for (Integer gId: gIds) {
                MarketGoods goods = goodsMapper.selectByPrimaryKey(gId);
                TopicReadGListVO topicReadGListVO = new TopicReadGListVO(goods.getId(),goods.getName(),goods.getBrief(),goods.getPicUrl(),
                        goods.getIsNew(),goods.getIsHot(),goods.getCounterPrice(),goods.getRetailPrice());
                goodslist.add(topicReadGListVO);
            }
        }
        return new TopicReadVO(goodslist,topicListInnerVO);
    }

    /**
     * @description:
     * @author zhizhuke
     * @date 2022-01-08 08-47
     * @version 1.0
     */
    @Override
    public TopicUpdateBVO update(TopicUpdateBVO to) {
        MarketTopic topic = new MarketTopic(to.getId(),to.getTitle(),to.getSubtitle(),new BigDecimal(to.getPrice()),to.getReadCount(),
                to.getPicUrl(),to.getSortOrder(),to.getGoods(),to.getAddTime(),to.getUpdateTime(), to.getDeleted() ,to.getContent());
        topicMapper.updateByPrimaryKey(topic);
        return to;
    }


    /**
     * @description:
     * @author zhizhuke
     * @date 2022-01-08 08-47
     * @version 1.0
     */
 //   @CacheEvict
    @Override
    public void delete(TopicUpdateBVO topicUpdateBVO) {
        MarketTopic topic = new MarketTopic();
        topic.setId(topicUpdateBVO.getId());
        topic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(topic);
    }
}
